package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.BuyRecord;
import cn.wolfcode.crm.mapper.BuyRecordMapper;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IBuyRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BuyRecordServiceImpl implements IBuyRecordService {

    @Autowired
    private BuyRecordMapper buyRecordMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        //传入当前页,以及每页显示条数
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<BuyRecord> list = buyRecordMapper.selectForList(qo);
        return new PageInfo(list);
    }


    @Override
    @Transactional
    public void save(BuyRecord buyRecord) {
        buyRecord.setInputTime(new Date());
        buyRecordMapper.insert(buyRecord);
        customerMapper.updateStatus(buyRecord.getCustomer().getId(),3,null);
        buyRecord.setId(10L);
        this.update(buyRecord);
    }

    @Override
    public void update(BuyRecord buyRecord) {
//        int a = 1/ 0;
        buyRecordMapper.updateByPrimaryKey(buyRecord);
    }

    @Override
    public BuyRecord get(Long id) {
        return buyRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BuyRecord> list() {
        return buyRecordMapper.selectAll();
    }
}
